home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 310 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.3 KB  |  79 lines

  1. Path: usin.com!rwells
  2. From: rwells@usin.com (Roger Wells)
  3. Newsgroups: comp.std.c
  4. Subject: Re: HELP!! Beginner's question...
  5. Date: 5 Feb 1996 23:14:49 GMT
  6. Organization: U.S. Intelco Networks, Inc.
  7. Distribution: world
  8. Message-ID: <4f6319$mev@news1.halcyon.com>
  9. References: <Pine.OSF.3.91l.960131005708.7552A-100000@saul3.u.washington.edu> <DM5LC6.M2n@uis.com>
  10. NNTP-Posting-Host: 198.202.216.7
  11.  
  12. In article <DM5LC6.M2n@uis.com>, scottm@uis.com (Scott Miller) writes:
  13. >
  14. >I compiled your program and ran it on our HP 700 series Unix machine,
  15. >entered 5 as the non-negative integer and 7 as the power, and got 78,125.
  16. >The program appears to be correct.
  17. >
  18. >This could be a system dependant problem -- different systems may use
  19. >different sizes for int and long int.
  20.  
  21. >Your instructor's advice to use long int sounds wise --
  22. >try it if you haven't already.
  23. >
  24. >                Scott Miller
  25. >
  26. >
  27. >>------------------------------------------------------------------------
  28. >
  29. >>#include <stdio.h>
  30. >>                  
  31. >>int main(void)
  32. >>{
  33. >>   int i,         /* integer */
  34. >>       n,         /* power to raise integer */
  35. >>       i_total,   /* integer's total value*/
  36. >>       count;     /* keeps track of number of loops run */
  37. >
  38. >>   /* asks for an integer and stores it in 'i' */
  39. >>   printf("Enter a non-negative integer: ");
  40. >>   scanf("%d", &i); 
  41. >
  42. >>   /* asks for a power and stores it in 'n' */
  43. >>   printf("What power should we raise it to? ");
  44. >>   scanf("%d", &n);
  45. >
  46. >>   /* if 'n' is negative, program will treat it as a '0' */    
  47. >>   if (n < 0){
  48. >>       n = 0;
  49. >>   }            
  50. >>                    
  51. >>    i_total = 1;       /* gives i_total an initial total of '1' */
  52. >
  53. >>    for (count = 1;  count <= n;  count = count + 1){
  54. >>        i_total = i_total * i;
  55. >>    }
  56. >
  57. >>    /* prints out the inputs and result */
  58. >>    printf("%d raised to the %dth power is %d\n\n", i, n, i_total); 
  59. >
  60. >>    return(0);    
  61. >>}
  62.  
  63. If (as I understand) he's changed i_total to type long and it still has
  64. this problem, you also have to change the format to %ld (for long int):
  65.  
  66. given:
  67.    int i;
  68.    int n;
  69.    long i_total;
  70.  
  71.  
  72.    printf("%d raised to the %dth power is %ld\n\n", i, n, i_total);
  73.  
  74. (I normally don't do homework problems; however, I suspect I'm saying
  75. exactly what the instructor would say.)
  76.  
  77. --
  78. Roger Wells  (speaking only for myself)
  79.